package com.ambimmort.shj.service.traffic.flow.group;

import cn.net.greenet.common.db_service.BaseDao;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Josh on 2015/7/31.
 */
@Service
public class TrafficFlowGroupService {
//    @Autowired
//    private SessionFactory hibernateSessionFactory;
            ResultSet rs =null;
    BaseDao baseDao = null;

    private void init() {
        baseDao = new BaseDao();
    }

    private void close() {
        if (baseDao != null) {
            baseDao.close();
        }
    }

    public String create_Traffic_flow(String json) throws SQLException {
        JSONObject jo = JSONObject.fromObject(json);
        String group_name = jo.getString("group_name"); //区域组名称
        JSONArray jor = jo.getJSONArray("areas");  //区域组名称
        int group_id = 0;
        String areas = "";
        for (int i = 0; i < jor.size(); i++) {
            areas += jor.getString(i) + ",";
        }
//        BaseDao baseDao = new BaseDao();
        init();
        int row = baseDao.executeUpdate("insert into traffic_flow_group_area(`group_name`,`areas`) values(?,?)", new Object[]{group_name, areas.substring(0, areas.length() - 1)});
        if (row != 0) {
            rs = baseDao.executeSQL("select id from traffic_flow_group_area where group_name = ?", new Object[]{group_name});
            while (rs.next()) {
                group_id = rs.getInt("id");
            }
            JSONObject joo = new JSONObject();
            joo.put("group_id", group_id);
            return joo.toString();
        }
//<<<<<<< HEAD

//=======
        close();
//>>>>>>> ec4760b9cf5cf03afd54f42712720e8b7196e337
        return null;
    }

    public int edit_Traffic(String json){
        JSONObject jo = JSONObject.fromObject(json);
        int old_group_id = Integer.parseInt(jo.getString("old_group_id"));
        String new_group_name = jo.getString("new_group_name");
        JSONArray jor = jo.getJSONArray("areas");  //区域组名称
        String areas = "";
        for (int i = 0; i < jor.size(); i++) {
            areas += jor.getString(i) + ",";
        }
//        BaseDao baseDao = new BaseDao();
        init();
        int row = baseDao.executeUpdate("update traffic_flow_group_area set `group_name` = ? ,`areas` = ? where id = ?", new Object[]{new_group_name, areas.substring(0, areas.length() - 1), old_group_id});
        close();
        return row;
    }

    public String query_Areas(String json) throws SQLException {
        JSONObject jo = JSONObject.fromObject(json);
        int group_id = Integer.parseInt(jo.getString("group_id"));
        String group_name = "";
//        BaseDao baseDao = new BaseDao();
        init();
        rs = baseDao.executeSQL("select group_name from traffic_flow_group_area where id = ?", new Object[]{group_id});
        while (rs.next()) {
            group_name = rs.getString("group_name");
        }
        JSONObject joo = new JSONObject();
        joo.put("group_name", group_name);
        close();
        return joo.toString();
    }
}
